<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/crop_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - crop_layer.cpp<span style="font-size: 80%;"> (source / <a href="crop_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">71</td>
            <td class="headerCovTableEntryLo">2.8 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">18</td>
            <td class="headerCovTableEntryLo">11.1 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;algorithm&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;functional&gt;
<span class="lineNum">       3 </span>            : #include &lt;map&gt;
<span class="lineNum">       4 </span>            : #include &lt;set&gt;
<span class="lineNum">       5 </span>            : #include &lt;vector&gt;
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &quot;caffe/layer.hpp&quot;
<span class="lineNum">       9 </span>            : #include &quot;caffe/layers/crop_layer.hpp&quot;
<span class="lineNum">      10 </span>            : #include &quot;caffe/net.hpp&quot;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : namespace caffe {
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      16 </span><span class="lineNoCov">          0 : void CropLayer&lt;Dtype&gt;::LayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      17 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      18 </span>            :   // LayerSetup() handles the number of dimensions; Reshape() handles the sizes.
<span class="lineNum">      19 </span>            :   // bottom[0] supplies the data
<span class="lineNum">      20 </span>            :   // bottom[1] supplies the size
<span class="lineNum">      21 </span>            :   const CropParameter&amp; param = this-&gt;layer_param_.crop_param();
<span class="lineNum">      22 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom.size(), 2) &lt;&lt; &quot;Wrong number of bottom blobs.&quot;;</span>
<span class="lineNum">      23 </span><span class="lineNoCov">          0 :   int input_dim = bottom[0]-&gt;num_axes();</span>
<span class="lineNum">      24 </span><span class="lineNoCov">          0 :   const int start_axis = bottom[0]-&gt;CanonicalAxisIndex(param.axis());</span>
<span class="lineNum">      25 </span><span class="lineNoCov">          0 :   CHECK_LT(start_axis, input_dim) &lt;&lt; &quot;crop axis bigger than input dim&quot;;</span>
<span class="lineNum">      26 </span><span class="lineNoCov">          0 :   if (param.offset_size() &gt; 1) {</span>
<span class="lineNum">      27 </span>            :     // the number of crop values specified must be equal to the number
<span class="lineNum">      28 </span>            :     // of dimensions following axis
<span class="lineNum">      29 </span><span class="lineNoCov">          0 :     CHECK_EQ(start_axis + param.offset_size(), input_dim)</span>
<span class="lineNum">      30 </span>            :       &lt;&lt; &quot;number of offset values specified must be equal to the number of &quot;
<span class="lineNum">      31 </span>            :       &lt;&lt; &quot;dimensions following axis.&quot;;
<span class="lineNum">      32 </span>            :   }
<span class="lineNum">      33 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      36 </span><span class="lineNoCov">          0 : void CropLayer&lt;Dtype&gt;::Reshape(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      37 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      38 </span>            :   const CropParameter&amp; param = this-&gt;layer_param_.crop_param();
<span class="lineNum">      39 </span><span class="lineNoCov">          0 :   int input_dim = bottom[0]-&gt;num_axes();</span>
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :   const int start_axis = bottom[0]-&gt;CanonicalAxisIndex(param.axis());</span>
<span class="lineNum">      41 </span>            : 
<span class="lineNum">      42 </span>            :   // Initialize offsets to 0 and the new shape to the current shape of the data.
<span class="lineNum">      43 </span><span class="lineNoCov">          0 :   vector&lt;int&gt; new_shape(bottom[0]-&gt;shape());</span>
<span class="lineNum">      44 </span><span class="lineNoCov">          0 :   vector&lt;int&gt; offsets_shape(1, input_dim);</span>
<span class="lineNum">      45 </span><span class="lineNoCov">          0 :   offsets.Reshape(offsets_shape);</span>
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :   int* offset_data = offsets.mutable_cpu_data();</span>
<span class="lineNum">      47 </span>            : 
<span class="lineNum">      48 </span>            :   // Determine crop offsets and the new shape post-crop.
<span class="lineNum">      49 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; input_dim; ++i) {</span>
<span class="lineNum">      50 </span>            :     int crop_offset = 0;
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :     int new_size = bottom[0]-&gt;shape(i);</span>
<span class="lineNum">      52 </span><span class="lineNoCov">          0 :     if (i &gt;= start_axis) {</span>
<span class="lineNum">      53 </span><span class="lineNoCov">          0 :       new_size = bottom[1]-&gt;shape(i);</span>
<span class="lineNum">      54 </span><span class="lineNoCov">          0 :       if (param.offset_size() == 1) {</span>
<span class="lineNum">      55 </span>            :         // If only one offset is given, all crops have the same offset.
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :         crop_offset = param.offset(0);</span>
<span class="lineNum">      57 </span><span class="lineNoCov">          0 :       } else if (param.offset_size() &gt; 1) {</span>
<span class="lineNum">      58 </span>            :         // For several offsets, the number of offsets must be equal to the
<span class="lineNum">      59 </span>            :         // number of dimensions to crop, that is dimensions after the axis.
<span class="lineNum">      60 </span><span class="lineNoCov">          0 :         crop_offset = param.offset(i - start_axis);</span>
<span class="lineNum">      61 </span>            :       }
<span class="lineNum">      62 </span>            :       // Check that the crop and offset are within the dimension's bounds.
<span class="lineNum">      63 </span><span class="lineNoCov">          0 :       CHECK_GE(bottom[0]-&gt;shape(i) - crop_offset, bottom[1]-&gt;shape(i))</span>
<span class="lineNum">      64 </span><span class="lineNoCov">          0 :           &lt;&lt; &quot;the crop for dimension &quot; &lt;&lt; i &lt;&lt; &quot; is out-of-bounds with &quot;</span>
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :           &lt;&lt; &quot;size &quot; &lt;&lt; bottom[1]-&gt;shape(i) &lt;&lt; &quot; and offset &quot; &lt;&lt; crop_offset;</span>
<span class="lineNum">      66 </span>            :     }
<span class="lineNum">      67 </span><span class="lineNoCov">          0 :     new_shape[i] = new_size;</span>
<span class="lineNum">      68 </span><span class="lineNoCov">          0 :     offset_data[i] = crop_offset;</span>
<span class="lineNum">      69 </span>            :   }
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :   top[0]-&gt;Reshape(new_shape);</span>
<span class="lineNum">      71 </span>            :   // Compute strides
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :   src_strides_.Reshape(offsets_shape);</span>
<span class="lineNum">      73 </span><span class="lineNoCov">          0 :   dest_strides_.Reshape(offsets_shape);</span>
<span class="lineNum">      74 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; input_dim; ++i) {</span>
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :     src_strides_.mutable_cpu_data()[i] = bottom[0]-&gt;count(i + 1, input_dim);</span>
<span class="lineNum">      76 </span><span class="lineNoCov">          0 :     dest_strides_.mutable_cpu_data()[i] = top[0]-&gt;count(i + 1, input_dim);</span>
<span class="lineNum">      77 </span>            :   }
<span class="lineNum">      78 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      79 </span>            : 
<span class="lineNum">      80 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      81 </span><span class="lineNoCov">          0 : void CropLayer&lt;Dtype&gt;::crop_copy(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      82 </span>            :              const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,
<span class="lineNum">      83 </span>            :              const int* offsets,
<span class="lineNum">      84 </span>            :              vector&lt;int&gt; indices,
<span class="lineNum">      85 </span>            :              int cur_dim,
<span class="lineNum">      86 </span>            :              const Dtype* src_data,
<span class="lineNum">      87 </span>            :              Dtype* dest_data,
<span class="lineNum">      88 </span>            :              bool is_forward) {
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :   if (cur_dim + 1 &lt; top[0]-&gt;num_axes()) {</span>
<span class="lineNum">      90 </span>            :     // We are not yet at the final dimension, call copy recursively
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; top[0]-&gt;shape(cur_dim); ++i) {</span>
<span class="lineNum">      92 </span><span class="lineNoCov">          0 :       indices[cur_dim] = i;</span>
<span class="lineNum">      93 </span><span class="lineNoCov">          0 :       crop_copy(bottom, top, offsets, indices, cur_dim+1,</span>
<span class="lineNum">      94 </span>            :                 src_data, dest_data, is_forward);
<span class="lineNum">      95 </span>            :     }
<span class="lineNum">      96 </span>            :   } else {
<span class="lineNum">      97 </span>            :     // We are at the last dimensions, which is stored continuously in memory
<span class="lineNum">      98 </span>            :     // prepare index vector reduced(red) and with offsets(off)
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :     std::vector&lt;int&gt; ind_red(cur_dim, 0);</span>
<span class="lineNum">     100 </span><span class="lineNoCov">          0 :     std::vector&lt;int&gt; ind_off(cur_dim+1, 0);</span>
<span class="lineNum">     101 </span><span class="lineNoCov">          0 :     for (int j = 0; j &lt; cur_dim; ++j) {</span>
<span class="lineNum">     102 </span><span class="lineNoCov">          0 :       ind_red[j] = indices[j];</span>
<span class="lineNum">     103 </span><span class="lineNoCov">          0 :       ind_off[j] = indices[j] + offsets[j];</span>
<span class="lineNum">     104 </span>            :     }
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :     ind_off[cur_dim] = offsets[cur_dim];</span>
<span class="lineNum">     106 </span>            :     // do the copy
<span class="lineNum">     107 </span><span class="lineNoCov">          0 :     if (is_forward) {</span>
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :       caffe_copy(top[0]-&gt;shape(cur_dim),</span>
<span class="lineNum">     109 </span><span class="lineNoCov">          0 :           src_data + bottom[0]-&gt;offset(ind_off),</span>
<span class="lineNum">     110 </span><span class="lineNoCov">          0 :           dest_data + top[0]-&gt;offset(ind_red));</span>
<span class="lineNum">     111 </span>            :     } else {
<span class="lineNum">     112 </span>            :       // in the backwards pass the src_data is top_diff
<span class="lineNum">     113 </span>            :       // and the dest_data is bottom_diff
<span class="lineNum">     114 </span><span class="lineNoCov">          0 :       caffe_copy(top[0]-&gt;shape(cur_dim),</span>
<span class="lineNum">     115 </span><span class="lineNoCov">          0 :           src_data + top[0]-&gt;offset(ind_red),</span>
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :           dest_data + bottom[0]-&gt;offset(ind_off));</span>
<span class="lineNum">     117 </span>            :     }
<span class="lineNum">     118 </span>            :   }
<span class="lineNum">     119 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     120 </span>            : 
<span class="lineNum">     121 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     122 </span><span class="lineNoCov">          0 : void CropLayer&lt;Dtype&gt;::Forward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">     123 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">     124 </span><span class="lineNoCov">          0 :   std::vector&lt;int&gt; indices(top[0]-&gt;num_axes(), 0);</span>
<span class="lineNum">     125 </span><span class="lineNoCov">          0 :   const Dtype* bottom_data = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">     126 </span><span class="lineNoCov">          0 :   Dtype* top_data = top[0]-&gt;mutable_cpu_data();</span>
<span class="lineNum">     127 </span><span class="lineNoCov">          0 :   crop_copy(bottom, top, offsets.cpu_data(), indices, 0, bottom_data, top_data,</span>
<span class="lineNum">     128 </span>            :       true);
<span class="lineNum">     129 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     130 </span>            : 
<span class="lineNum">     131 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     132 </span><span class="lineNoCov">          0 : void CropLayer&lt;Dtype&gt;::Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">     133 </span>            :     const vector&lt;bool&gt;&amp; propagate_down, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">     134 </span><span class="lineNoCov">          0 :   const Dtype* top_diff = top[0]-&gt;cpu_diff();</span>
<span class="lineNum">     135 </span><span class="lineNoCov">          0 :   Dtype* bottom_diff = bottom[0]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">     136 </span>            : 
<span class="lineNum">     137 </span><span class="lineNoCov">          0 :   if (propagate_down[0]) {</span>
<span class="lineNum">     138 </span><span class="lineNoCov">          0 :     caffe_set(bottom[0]-&gt;count(), static_cast&lt;Dtype&gt;(0), bottom_diff);</span>
<span class="lineNum">     139 </span><span class="lineNoCov">          0 :     std::vector&lt;int&gt; indices(top[0]-&gt;num_axes(), 0);</span>
<span class="lineNum">     140 </span><span class="lineNoCov">          0 :     crop_copy(bottom, top, offsets.cpu_data(), indices, 0, top_diff,</span>
<span class="lineNum">     141 </span>            :         bottom_diff, false);
<span class="lineNum">     142 </span>            :   }
<span class="lineNum">     143 </span><span class="lineNoCov">          0 : }</span>
<a name="144"><span class="lineNum">     144 </span>            : </a>
<span class="lineNum">     145 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     146 </span><span class="lineNoCov">          0 : STUB_GPU(CropLayer);</span>
<span class="lineNum">     147 </span>            : #endif
<a name="148"><span class="lineNum">     148 </span>            : </a>
<span class="lineNum">     149 </span>            : INSTANTIATE_CLASS(CropLayer);
<a name="150"><span class="lineNum">     150 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(Crop);</span></a>
<span class="lineNum">     151 </span>            : 
<span class="lineNum">     152 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
